package com.sq.partner.manager.util;

import java.io.UnsupportedEncodingException;

import org.jasypt.util.text.BasicTextEncryptor;

public class EncryptUtil {
	private static BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    private static String PASSWORD = "MANAGE@CIC";
    
    static{
    	textEncryptor.setPassword(PASSWORD);
    }
    
    /**
     * 加密
     *
     * @param password
     * @return
     */
    public static String encrypt(String text) {
    	return textEncryptor.encrypt(text);
    }

    /**
     * 解密
     *
     * @param encryptionPassword
     * @return
     */
    public static String decrypt(String text) {
    	
    	return textEncryptor.decrypt(text);
    }
    
    
    /**
     * 将 s 进行 BASE64 编码
     * @return String
     */
    public static String base64Encode(String s) {
        if (s == null) return null;
        String res = "";
        try {
            res = new sun.misc.BASE64Encoder().encode(s.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 将 BASE64 编码的字符串 s 进行解码
     *
     * @return String
     */
    public static String base64Decode(String s) {
        if (s == null) return null;
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(s);
            return new String(b,"UTF-8");
        } catch (Exception e) {
            return null;
        }
    }
 
    
    public static void main(String args[]){
    	String data="user=a#$*()sdfjks&pwd=abcd!~";
		String data2="{\n" +
					" USER:\"JSCD\",\n" + 
					" PWD:\"JSCD@PTN\",\n" +
					" CHANNEL_CODE:\"JSCD\",\n" + 
					" CHANNLE_CODE:\"A\",\n" + 
					" CHANNLE_TYPE:\"A01\",\n" + 
					" NEW_BSNS_TYPE:\"A0101\",\n" + 
					" CATEGORY_CODE:\"19001\",\n" + 
					" DPT_CDE:\"37030005\",\n" + 
					" EMP_NO:\"37031128\",\n" + 
					" EMP_NAME:\"ss\",\n" + 
					" TEAM_CODE:\"37030014\",\n" + 
					" TEAM_NAME:\"ss\",\n" + 
					" AGENT_NO:\"A0909\",\n" + 
					" PROTOCOL_NO:\"asd\",\n" + 
					" C_BRKR_NME:\"ddd\",\n" + 
					" SERVICE_CODE:\"21\",\n" + 
					" SERVICE_NAME:\"ss\",\n" + 
					" KEY1:\"321\",\n" + 
					" KEY2:\"123\",\n" + 
					" KEY3:\"312\",\n" + 
					" KEY4:\"12\",\n" + 
					" KEY5:\"key233\"\n" + 
					"}";

    	String en=encrypt(data2);
    	try {
    		System.out.println("*************************");
    		System.out.println("加密后： "+en);
			String aa=EncryptUtil.base64Encode(en);
			System.out.println("编码后： "+aa);
			System.out.println("编码后字符长度： "+aa.length());
			
			String aa1=EncryptUtil.base64Decode(aa);
			System.out.println("解码后： "+aa1);
			System.out.println("解密后："+decrypt(aa1));
		} catch (Exception e) {
			e.printStackTrace();
		}  
    	
    }

}
